package com.zhiyiang.service.type;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhiyiang.Vo.TypeDataVO;
import com.zhiyiang.mapper.HotelMapper;
import com.zhiyiang.mapper.RoomMapper;
import com.zhiyiang.mapper.TypeMapper;
import com.zhiyiang.mapper.UserMapper;
import com.zhiyiang.pojo.Hotel;
import com.zhiyiang.pojo.Room;
import com.zhiyiang.pojo.Type;
import com.zhiyiang.pojo.User;
import com.zhiyiang.util.ColorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HotelMapper hotelMapper;
    @Override
    public List<Type> getTypes(Integer hotelid) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("roomtype");
        queryWrapper.eq("hotelid", hotelid).groupBy("roomtype");
        List<Room> rooms = roomMapper.selectList(queryWrapper);
        List<Type> types = new ArrayList<>();
        for (Room room:
             rooms) {
            Type type = typeMapper.selectById(room.getRoomtype());
            System.out.println(type);
            types.add(type);
        }
        return types;
    }
    @Override
    public List<Type> getTypeList() {
        return typeMapper.selectList(null);
    }

    @Override
    public void addType(Type type) {
        typeMapper.insert(type);
    }

    @Override
    public Type getTypeById(Integer id) {
        return typeMapper.selectById(id);
    }

    @Override
    public void updateType(Type type) {
        typeMapper.updateById(type);
    }

    @Override
    public void deleteType(Integer id) {
        typeMapper.deleteById(id);
    }

    @Override
    public List<TypeDataVO> getTypeData (String usercode) {
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        if (user.getRole()==2){
            QueryWrapper<Hotel> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userid", user.getId());
            Hotel hotel = hotelMapper.selectOne(queryWrapper2);
            queryWrapper.eq("hotelid", hotel.getId());
        }
        queryWrapper.select("roomtype as name","count(*) as value").groupBy("roomtype");
        List<Room> rooms = roomMapper.selectList(queryWrapper);
        List<TypeDataVO> list = new ArrayList<>();
        for (Room room:
                rooms) {
            Type type = typeMapper.selectById(room.getName());
            TypeDataVO typeDataVO = new TypeDataVO();
            typeDataVO.setName(type.getTypename());
            typeDataVO.setValue(room.getValue());
            typeDataVO.setItemStyle(ColorUtil.getRandColorCode());
            list.add(typeDataVO);
        }
        return list;
    }
}
